package com.and.bean;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import org.compass.annotations.Searchable;
/**
 * system 管理
 * gouanming
 * gouanming11@yahoo.com.cn
 * 安的软件�?��
 */
@Entity
@Searchable(root = false)
// 通过这个注解,指明该类跟lucene中的document映射
public class Users implements Serializable {
	private static final long serialVersionUID = -4540465642606278764L;

	private Integer id;
	private String username;

	private String password;
	private Integer status;
	private String descn;
	
	private Set<Role> role = new HashSet<Role>();
	
	
	public Users(){}
	
	
	
	
	
	
	
	public Users(String username, String password, Integer status) {
		super();
		this.username = username;
		this.password = password;
		this.status = status;
	}

	

	@Column(length = 15, nullable = false)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@ManyToMany(cascade = CascadeType.REFRESH)
	@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	public Set<Role> getRole() {
		return role;
	}

	public void setRole(Set<Role> role) {
		this.role = role;
	}

	@Column(length = 15, nullable = false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getDescn() {
		return descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
/**
 * add role
 * @param r
 */
	public void addRole(Role r) {
		this.role.add(r);

	}
/**
 * remove role
 * @param r
 */
	public void remRole(Role r) {
		if (this.role.contains(r)) {
			this.role.remove(r);
		}

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Users other = (Users) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}


}
